package org.solrext.handler;

import java.io.IOException;

import org.apache.solr.common.params.SolrParams;
import org.apache.solr.core.SolrCore;
import org.apache.solr.handler.RequestHandlerBase;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrQueryResponse;
import org.apache.solr.update.CommitUpdateCommand;
import org.apache.solr.update.DeleteUpdateCommand;
import org.apache.solr.update.processor.UpdateRequestProcessor;
import org.apache.solr.update.processor.UpdateRequestProcessorChain;

/**
 * @author Uri Boness
 */
public class DeleteRequestHandler extends RequestHandlerBase {

    @Override
    public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
        SolrCore core = SolrCore.getSolrCore();
        UpdateRequestProcessorChain processorFactory = core.getUpdateProcessingChain(null);

        UpdateRequestProcessor processor = processorFactory.createProcessor(req, rsp);
        processDelete(processor, req.getParams());
        processor.finish();
    }


    //============================================== Helper Methods ====================================================

    private void processDelete(UpdateRequestProcessor processor, SolrParams params) throws IOException {
        String id = params.get("id");
        String query = params.get("query");
        if (id == null && query == null) {
            throw new RuntimeException("Expecting either a document id or a query");
        }
        boolean fromPending = params.getBool("fromPending", true);
        boolean fromCommited = params.getBool("fromCommitted", true);

        DeleteUpdateCommand delete = new DeleteUpdateCommand();
        delete.fromPending = fromPending;
        delete.fromCommitted = fromCommited;
        if (id != null) {
            delete.id = id;
        } else {
            delete.query = query;
        }
        processor.processDelete(delete);

        CommitUpdateCommand commit = resolveCommit(params);
        if (commit != null) {
            processor.processCommit(commit);
        }
    }

    private CommitUpdateCommand resolveCommit(SolrParams params) {
        boolean commit = params.getBool("commit", false);
        if (!commit) {
            return null;
        }
        CommitUpdateCommand cmd = new CommitUpdateCommand(params.getBool("optimize", false));
        cmd.waitFlush = params.getBool("waitFlush", false);
        cmd.waitSearcher = params.getBool("waitSearcher", false);

        return cmd;
    }

    //============================================== JMX Methods ====================================================

    @Override
    public String getDescription() {
        return DeleteRequestHandler.class.getSimpleName();
    }

    @Override
    public String getSourceId() {
        return DeleteRequestHandler.class.getName();
    }

    @Override
    public String getSource() {
        return DeleteRequestHandler.class.getName();
    }

    @Override
    public String getVersion() {
        return "";
    }
}
